from fastapi import Request
from sqlalchemy.orm import Session

from core.dao.cms_alert_rule_dao import CmsAlertRuleDao
from core.entity.vo.common import CrudResponse
from core.entity.vo.view_models import CmsAlertRuleVO


class CmsAlertRuleService:
    """
    高德终端service
    """

    @classmethod
    def get_cms_alert_rule_by_id(cls, result_db: Session, id: int):
        """
        获取参数配置列表信息service
        :param result_db: orm对象
        :param id: id
        :return: 参数配置列表信息对象
        """
        cms_alert_rule = CmsAlertRuleDao.get_cms_alert_rule_by_id(result_db, id)

        return cms_alert_rule

    @classmethod
    def get_cms_alert_rule_by_fence_id(cls, result_db: Session, fence_id: str, rule_type: str):
        """
        获取参数配置列表信息service
        :param result_db: orm对象
        :param fence_id: 高德终端id
        :return: 参数配置列表信息对象
        """
        cms_alert_rule = CmsAlertRuleDao.get_cms_alert_rule_by_fence_id(result_db, fence_id, rule_type)

        return cms_alert_rule

    @classmethod
    def get_cms_alert_rule_like_name(cls, result_db: Session, rule_name: str):
        """
        获取参数配置列表信息service
        :param result_db: orm对象
        :param query_object: 查询参数对象
        :return: 参数配置列表信息对象
        """
        cms_alert_rule_list = CmsAlertRuleDao.get_cms_alert_rule_like_name(result_db, rule_name)

        return cms_alert_rule_list

    @classmethod
    async def add_cms_alert_rule(cls, request: Request, result_db: Session, cms_alert_rule: CmsAlertRuleVO):
        """
        新增参数配置信息service
        :param request: Request对象
        :param result_db: orm对象
        :param cms_alert_rule: 新增参数配置对象
        :return: 新增参数配置校验结果
        """
        try:
            CmsAlertRuleDao.add_cms_alert_rule_dao(result_db, cms_alert_rule)
            result_db.commit()
            result = dict(is_success=True, message='新增成功')
        except Exception as e:
            result_db.rollback()
            result = dict(is_success=False, message=str(e))
        return CrudResponse(**result)

    @classmethod
    async def edit_cms_alert_rule(cls, request: Request, result_db: Session, cms_alert_rule: CmsAlertRuleVO):
        """
        编辑参数配置信息service
        :param request: Request对象
        :param result_db: orm对象
        :param cms_alert_rule: 编辑参数配置对象
        :return: 编辑参数配置校验结果
        """
        try:
            edit_post = cms_alert_rule.dict(exclude_unset=True)
            CmsAlertRuleDao.edit_cms_alert_rule_dao(result_db, edit_post)
            result_db.commit()
            result = dict(is_success=True, message='修改成功')
        except Exception as e:
            result_db.rollback()
            result = dict(is_success=False, message=str(e))
        return CrudResponse(**result)

    @classmethod
    def delete_cms_alert_rule(cls, result_db: Session, id: int):
        """
        删除菜单信息service
        :param result_db: orm对象
        :param id: id
        :return: 删除终端
        """
        if id > 0:
            try:
                CmsAlertRuleDao.delete_cms_alert_rule_dao(result_db, id)
                result_db.commit()
                result = dict(is_success=True, message='删除成功')
            except Exception as e:
                result_db.rollback()
                result = dict(is_success=False, message=str(e))
        else:
            result = dict(is_success=False, message='传入id为空')
        return CrudResponse(**result)
